<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.simpleforum.model.Post" %>
<%@ page import="com.example.simpleforum.model.Forum" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>帖子列表 - 极简论坛</title>
    <link rel="stylesheet" href="css/style.css">
<body>
<div class="container">
    <h1>帖子列表</h1>
    <div class="actions">
        <a href="index.jsp" class="btn">返回首页</a>
        <a href="PostAddServlet" class="btn">发布新帖</a>
    </div>
    
    <%-- 搜索功能已移除 --%>
    
    <%-- 移除搜索结果展示区块，直接跳转到板块帖子列表 --%>
    
    <% if (request.getAttribute("forums") != null) { %>
    <div class="forum-filter">
        <form action="PostListServlet" method="get">
            <select name="forumId" onchange="this.form.submit()">
                <option value="">-- 所有板块 --</option>
                <% 
                    List<Forum> forums = (List<Forum>) request.getAttribute("forums");
                    Integer currentForumId = (Integer) request.getAttribute("forumId");
                    for (Forum forum : forums) { 
                %>
                <option value="<%= forum.getId() %>" 
                    <%= currentForumId != null && currentForumId == forum.getId() ? "selected" : "" %>>
                    <%= forum.getName() %>
                </option>
                <% } %>
            </select>
        </form>
    </div>
    <% } %>
    <div class="post-list">
        <% 
            List<Post> posts = (List<Post>) request.getAttribute("posts");
            if (posts != null && !posts.isEmpty()) {
                for (Post post : posts) {
        %>
        <div class="post-item">
            <h3><a href="PostDetailServlet?id=<%= post.getId() %>"><%= post.getTitle() %></a></h3>
            <div class="post-meta">
                <span>作者: <%= post.getUsername() %></span>
                <span>发布时间: <%= post.getCreateTime() %></span>
                <% if (post.getForumId() > 0) { 
                    String forumName = (String)request.getAttribute("forumName_"+post.getForumId());
                    if (forumName != null) { %>
                <span>板块: <a href="PostListServlet?forumId=<%= post.getForumId() %>"><%= forumName %></a></span>
                <% } } %>
            </div>
        </div>
        <% 
                }
            } else {
        %>
        <p>暂无帖子</p>
        <% } %>
    </div>
</div>
</body>
</html>